﻿using System.Windows.Controls;
using Red.Controls.ViewModels;
using Red.EngineData.View;

namespace Red.Controls
{
    /// <summary>
    /// Interaction logic for SnapControl.xaml
    /// </summary>
    public partial class SnapControl : UserControl
    {
        #region c-tor

        public SnapControl()
        {
            InitializeComponent();
        }

        #endregion

        #region Event Handlers

        private async void PositionSnapCommand_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            bool value = true.Equals( _positionSnapButton.IsChecked );
            _positionSnapGranularityDropDown.IsEnabled = value;
            if ( DataContext is DataItem )
            {
                await ( (DataItem)DataContext ).SetSubValueAsync( "snap.translationEnabled", value );
            }
        }

        private async void RotationSnapCommand_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            bool value = true.Equals( _rotationSnapButton.IsChecked );
            _rotationSnapGranularityDropDown.IsEnabled = value;
            if ( DataContext is DataItem )
            {
                await( (DataItem)DataContext ).SetSubValueAsync( "snap.rotationEnabled", value );
            }
        }

        private async void ScaleSnapCommand_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            bool value = true.Equals( _scaleSnapButton.IsChecked );
            _scaleSnapGranularityDropDown.IsEnabled = value;
            if ( DataContext is DataItem )
            {
                await( (DataItem)DataContext ).SetSubValueAsync( "snap.scalingEnabled", value );
            }
        }

        private async void PositionSnapGranularity_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ( DataContext is DataItem )
            {
                ComboBoxItem item = (ComboBoxItem)_positionSnapGranularityDropDown.SelectedItem;
                await ( (DataItem)DataContext ).SetSubValueAsync( "grid.positionGrid", item.Content.ToString() );
            }
        }

        private async void RotationSnapGranularity_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ( DataContext is DataItem )
            {
                ComboBoxItem item = (ComboBoxItem)_rotationSnapGranularityDropDown.SelectedItem;
                await( (DataItem)DataContext ).SetSubValueAsync( "grid.rotationGrid", item.Content.ToString() );
            }
        }

        private async void ScaleSnapGranularity_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ( DataContext is DataItem )
            {
                ComboBoxItem item = (ComboBoxItem)_scaleSnapGranularityDropDown.SelectedItem;
                await ( (DataItem)DataContext ).SetSubValueAsync( "grid.scalingGrid", item.Content.ToString() );
            }
        }

        #endregion
    }
}
